/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestModule.h"

namespace ModularEngine
{

	void TestModule::SENDCONSOLECMD::operator () ()
	{
		if( TestModule::getSingleton().userCommand->msText[0] == '/' )
			ConsoleModule::getSingleton().sendCommand( TestModule::getSingleton().userCommand->getText().substr( 1, TestModule::getSingleton().userCommand->getText().length()-1 ) );
		else
		{
			ClientModule::getSingleton().sendMessage( GM_CHAT, TestModule::getSingleton().userCommand->msText.c_str(), TestModule::getSingleton().userCommand->msText.length()+1 ); 
		}

		TestModule::getSingleton().userCommand->setText( "" );			
	}

	void TestModule::HOSTSERVER::startHosting::operator () ()
	{
		// start hosting
		console( "Hosting Server..." );
		TestModule::getSingleton().mConsoleWindow->mbVisible = true;
		TestModule::HOSTSERVER::getSingleton().mHostServerWindow->mbVisible = false;
		if( !ServerModule::getSingleton().initHost() )
		{
			console( "Shutting Server Down..." );
		}

		ClientModule::getSingleton().ipAddress = "127.0.0.1";

		if( !ClientModule::getSingleton().initClient() )
		{
			console( "Could not connect to the server." );
		}

		ClientModule::getSingleton().sendMessage( GM_NEWUSER );
	}

	void TestModule::initWindows()
	{
		ADDCOMMAND( cCLEAR );
	
		// Create Console Window
		WND( mConsoleWindow, Vector2d( 3, 768-150-40 ), Vector2d( 1024-15, 150 ), "Chat:" );
		// hidden by default
		mConsoleWindow->mbVisible = false;

		// The Console Textbox
		TB( consoleText, Vector2d( 5, 5 ), Vector2d( 1024-28, 120-29 ) );
		consoleText->mbMultiline = true;
		consoleText->mbInput = false;

		// Add the console textbox to the console window.
		mConsoleWindow->addElement( consoleText );

		// Create the input console textbox
		TB( userCommand, Vector2d( 5, 120-19 ), Vector2d( 1024-31-75, 23 ) );
		userCommand->mtFunc = &sendConsoleCmd;
		userCommand->bMemory = true;
		mConsoleWindow->addElement( userCommand );

		// The Button for the console:
		BTN( sendConsole, Vector2d( 1024-26-70, 120-19 ), Vector2d( 75, 23 ), "Send" );		
		sendConsole->setCallFunc( &sendConsoleCmd );
		mConsoleWindow->addElement( sendConsole );

		// Add the console window
		ADDELEMENT( mConsoleWindow );

		// The main menu
		WND( mMainMenu, Vector2d( 1024/2-110, 768/2-87 ), Vector2d( 110*2, 87*2 ), "Menu: " );

		int i = 0;
#define Y_BTN 10+28*i++
		BTN( connectToServer, Vector2d( 10, Y_BTN ),Vector2d( 200, 23 ), "Connect To Server..." );
		connectToServer->setCallFunc( &CONNECTTOSERVER::getSingleton() );
		BTN( hostServer, Vector2d( 10, Y_BTN ), Vector2d( 200, 23 ), "Host Server..." );
		hostServer->setCallFunc( &HOSTSERVER::getSingleton() );
		BTN( disconnect, Vector2d( 10, Y_BTN ), Vector2d( 200, 23 ), "Disconnect..." );
		//disconnect->setCallFunc();

		BTN( showSettings, Vector2d( 10, Y_BTN ), Vector2d( 200, 23 ), "Settings" );
		showSettings->setCallFunc( &showSettingsF );
		BTN( exit, Vector2d( 10, Y_BTN ), Vector2d( 200, 23 ), "Quit" );
		exit->setCallFunc( &exitF );
		
		mMainMenu->addElement( connectToServer );
		mMainMenu->addElement( hostServer );
		mMainMenu->addElement( disconnect );
		mMainMenu->addElement( showSettings );
		mMainMenu->addElement( exit );

		// Add the main menu
		ADDELEMENT( mMainMenu );
	}


	void TestModule::HOSTSERVER::operator () ()
	{
		if( !mHostServerWindow )
		{
			// open server window

			// Hide Main menu
			TestModule::getSingleton().mMainMenu->mbVisible = false;

			Vector2d winDims = WindowModule::getSingleton().getDimensions();
			WND( mHostServerWindow, Vector2d( winDims.x/2-200, winDims.y/2-65 ), Vector2d( 400, 130 ), "Host Server..." );

			BTN( hostServer, Vector2d( 265, 80 ), Vector2d( 125, 23 ), "Host Server" );
			hostServer->setCallFunc( &startHosting::getSingleton() );

			BTN( cancelButton, Vector2d( 175, 80 ), Vector2d( 85, 23 ), "Cancel" );
			cancelButton->setCallFunc( &cancel::getSingleton() );

			adminUserNameLabel = new HUDLabel();
			adminUserNameLabel->mvPosition = Vector2d( 10, 25 );
			isAdminLabel = new HUDLabel();					
			isAdminLabel->mvPosition = Vector2d( 10, 10 );

			isAdminLabel->setText( "Are you admin?" );
			adminUserNameLabel->setText( "If not, please enter his/her username:" );

			// Add server password
			// Add admin password.

			isAdmin = new HUDCheckbox();
			isAdmin->setCheck( true );
			isAdmin->mvPosition = Vector2d( 140, 14 );

			TB( adminUsername, Vector2d( 10, 47 ), Vector2d( 290, 23 ) );
		
			mHostServerWindow->addElement( cancelButton );
			mHostServerWindow->addElement( adminUsername );
			mHostServerWindow->addElement( adminUserNameLabel );
			mHostServerWindow->addElement( isAdminLabel );
			mHostServerWindow->addElement( isAdmin );
			mHostServerWindow->addElement( hostServer );

			ADDELEMENT( mHostServerWindow );

		} else {
			if( !mHostServerWindow->mbVisible )
				mHostServerWindow->mbVisible = true;
			TestModule::getSingleton().mMainMenu->mbVisible = false;
		}
	}

	void TestModule::CONNECTTOSERVER::operator () ()
	{
		if( !connectWindow )
			{
				// open server window

				// Hide Main menu
				TestModule::getSingleton().mMainMenu->mbVisible = false;

				Vector2d winDims = WindowModule::getSingleton().getDimensions();
				WND( connectWindow, Vector2d( winDims.x/2-200, winDims.y/2-45 ), Vector2d( 400, 90 ), "Connect To Server..." );

				ipLabel = new HUDLabel();
				ipLabel->setText( "IP Address: " );
				ipLabel->mvPosition = Vector2d( 10, 10 );
				

				TB( ipText, Vector2d( 125, 10 ), Vector2d( 260, 23 ) );
				ipText->mtFunc = &connectToHost::getSingleton();
				
#ifdef DEBUG_MD

				ipText->setText( "127.0.0.1" );

#endif



				BTN( connectButton, Vector2d( 220, 38 ), Vector2d( 125+40, 23 ), "Connect To Server" );
				connectButton->setCallFunc( &connectToHost::getSingleton() );

				BTN( cancelButton, Vector2d( 130, 38 ), Vector2d( 85, 23 ), "Cancel" );
				cancelButton->setCallFunc( &cancel::getSingleton() );

				connectWindow->addElement( ipText );
				connectWindow->addElement( ipLabel );
				connectWindow->addElement( cancelButton );
				connectWindow->addElement( connectButton );

				ADDELEMENT( connectWindow );

			} else {
				if( !connectWindow->mbVisible )
					connectWindow->mbVisible = true;
				TestModule::getSingleton().mMainMenu->mbVisible = false;
			}
	}

}

